package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Override
    public List<Map<String, Object>> getMenuListByUsername(String username) {
        List<Map<String, Object>> menuList = new ArrayList<>();
        List<Menu> parentMenus = menuDao.findParentMenu(username);
        List<Menu> childrenMenus = menuDao.findChildrenMenu(username);
        for (Menu parentMenu : parentMenus) {
            Map<String, Object> map = new HashMap<>();
            List<Map<String, Object>> childrenList = new ArrayList<>();
            for (Menu childrenMenu : childrenMenus) {
                Map<String, Object> childrenMap = new HashMap<>();
                if (childrenMenu.getParentMenuId().equals(parentMenu.getId())){
                    childrenMap.put("path",childrenMenu.getPath());
                    childrenMap.put("title",childrenMenu.getName());
                    childrenMap.put("linkUrl",childrenMenu.getLinkUrl());
                    childrenMap.put("children",childrenMenu.getChildren());
                    childrenList.add(childrenMap);
                }
            }
            map.put("path",parentMenu.getPath());
            map.put("title",parentMenu.getName());
            map.put("icon",parentMenu.getIcon());
            map.put("children",childrenList);
            menuList.add(map);
        }
        return menuList;
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        List<Map<String, Object>> dataList = new ArrayList<>();
        //开启分页查询
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //根据条件分页查询主菜单信息
        Page<Menu> page = menuDao.selectByCondition(queryPageBean.getQueryString());
        List<Menu> parentMenus = page.getResult();
        //查询所有的子菜单
        List<Menu> childrenMenus = menuDao.findAllChildrenMenu();
        for (Menu parentMenu : parentMenus) {
            Map<String, Object> map = new HashMap<>();
            List<Map<String, Object>> childrenList = new ArrayList<>();
            for (Menu childrenMenu : childrenMenus) {
                Map<String, Object> childrenMap = new HashMap<>();
                if (childrenMenu.getParentMenuId().equals(parentMenu.getId())){
                    childrenMap.put("id",childrenMenu.getId());
                    childrenMap.put("name",childrenMenu.getName());
                    childrenMap.put("linkUrl",childrenMenu.getLinkUrl());
                    childrenMap.put("priority",childrenMenu.getPriority());
                    childrenMap.put("parentMenuId",childrenMenu.getParentMenuId());
                    childrenMap.put("level",childrenMenu.getLevel());
                    childrenList.add(childrenMap);
                }
            }
            map.put("id",parentMenu.getId());
            map.put("name",parentMenu.getName());
            map.put("linkUrl",parentMenu.getLinkUrl());
            map.put("priority",parentMenu.getPriority());
            map.put("parentMenuId",parentMenu.getParentMenuId());
            map.put("level",parentMenu.getLevel());
            map.put("children",childrenList);
            dataList.add(map);
        }
        //构建分页数据返回
        return new PageResult(page.getTotal(),dataList);
    }

    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    @Override
    public void edit(Menu menu) {
        menuDao.update(menu);
    }

    @Override
    public void deleteById(Integer id) {
        //判断该菜单是否被角色表应用
        int count = menuDao.selectRoleReferencedCount(id);
        if(count>0){
            throw new RuntimeException("当前菜单项被角色表引用，不能删除 !");
        }
        //判断该菜单是否为父菜单,是否被子菜单应用
        int count1 = menuDao.selectMenuReferencedCount(id);
        if(count1>0){
            throw new RuntimeException("当前菜单项为父菜单，不能删除 !");
        }
        menuDao.deleteById(id);
    }

    @Override
    public List<Menu> findAll() {
        List<Menu> list = menuDao.findAll();
        return list;
    }
//查询菜单列表

}
